/*
 * @Description: 更新
 * @Author: Zzt
 * @Date: 2022-12-21 17:08:29
 * @LastEditTime: 2023-01-03 17:17:57
 */
import { Button, Form, Input, Modal, Select, Space } from 'antd';
import { useEffect, useMemo, useState } from 'react';
import { useBusinessGuidanceContext } from '../../contexts/BusinessGuidanceContext';
import { useGuidanceTypeContext } from '../../contexts/GuidanceWaysContext';
import FormFeedback from './FormFeedback';
import FormInspection from './FormInspection ';
import FromList from './FromList';
import TabelList from './TabelList';

const UpLoadBusiness = (props: any) => {
  const { visible, onClose, disabled, data } = props;
  const { type, title } = useGuidanceTypeContext();
  const { selectUnit, editBusinessList, ClearAnnex, enclosureList } = useBusinessGuidanceContext();

  const [form] = Form.useForm();
  useEffect(() => {
    if (visible) {
      form.setFieldsValue({ ...data });
    } else {
      // console.log('sssssa', enclosure);
      form.resetFields();
      ClearAnnex();
    }
  }, [visible, data]);
  const onCancel = () => {
    onClose();
  };
  /**
   * @description 提交表单，触发onFinish方法
   */
  const submitFunc = () => {
    form.submit();
  };

  // 修改调用方法
  const onFinish = (values: any) => {
    editBusinessList(
      type === 3
        ? { ...data, ...values, checkAttachmentJsonDTOS: enclosureList }
        : { ...data, ...values, attachmentJsonDTOS: enclosureList },
      type
    ).then(onClose());
  };

  return (
    <>
      <Modal
        title="修改批次"
        visible={visible}
        width={600}
        destroyOnClose
        onCancel={onCancel}
        footer={[
          <Button key="close" onClick={onCancel}>
            关闭
          </Button>,
          <Button
            key="send"
            type="primary"
            onClick={() => {
              submitFunc();
            }}
          >
            发送
          </Button>
        ]}
      >
        <Form
          labelCol={{ span: 5 }}
          wrapperCol={{ span: 19 }}
          preserve={false}
          form={form}
          onFinish={onFinish}
          labelAlign={'left'}
          // disabled={disabled}
        >
          {type === 1 && <FromList selectUnit={selectUnit} />}
          {type === 2 && <FormFeedback />}
          {type === 3 && <FormInspection />}
          <TabelList />
        </Form>
      </Modal>
    </>
  );
};
export default UpLoadBusiness;
